package com.tanhua.dubbo.server.controller;

import com.tanhua.dubbo.server.enums.CommentPublishIdType;
import com.tanhua.dubbo.server.service.QuanZiService;
import com.tanhua.dubbo.server.service.VideoService;
import com.tanhua.dubbo.server.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 *
 * @ClassName: VideoController
 * @Author:王晨晗
 * @Description: 小视频功能
 * @Date: 2022/01/03/14:26
 */
@RestController
@RequestMapping("smallVideos")
public class VideoController {
    @Autowired
    private VideoService videoService;
    @Autowired
    private QuanZiService quanZiService;

    /**
     * @description 小视频发布功能
     * @param picFile   封面图片
     * @param videoFile    视频图片
     * @return  失败则抛出异常
     */
    @PostMapping
    public ResponseEntity saveVideo(@RequestParam("videoThumbnail") MultipartFile picFile,
                                    @RequestParam("videoFile") MultipartFile videoFile){
        videoService.saveVideo(picFile , videoFile);
        return ResponseEntity.ok(null);
    }

    /**
     * @description 小视频列表
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping
    public ResponseEntity<PageResult> queryVideoList(@RequestParam("page") Integer  page,
                                                     @RequestParam("pagesize") Integer pageSize){
        PageResult pageResult = videoService.queryVideoList(page , pageSize);
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 视频点赞
     * @param videoId 视频id
     * @return
     */
    @PostMapping("/{id}/like")
    public ResponseEntity<Long> likeComment(@PathVariable("id") String videoId) {
        //调用点赞方法,返回点赞数
        Long count = videoService.likeComment(videoId);
        return ResponseEntity.ok(count);
    }

    /**
     * @description 取消点赞
     * @param videoId
     * @return
     */
    @PostMapping("/{id}/dislike")
    public ResponseEntity<Long> disLikeComment(@PathVariable("id") String videoId) {
        //调用取消点赞方法,返回点赞数
        Long count = videoService.disLikeComment(videoId);
        return ResponseEntity.ok(count);
    }

    /**
     * @description 获取评论列表
     * @param videoId   视频id
     * @param page      当前页
     * @param pageSize  每页条数
     * @return
     */
    @GetMapping("/{id}/comments")
    public ResponseEntity<PageResult> queryCommentsList(@PathVariable("id") String videoId,
                                                        @RequestParam(value = "page", defaultValue = "1") Integer page,
                                                        @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize) {
        //查询评论列表
        PageResult pageResult = videoService.queryCommentsList(videoId , page , pageSize);
        return ResponseEntity.ok(pageResult);
    }


    /**
     * @description 保存评论
     * @param param  参数列表
     * @param videoId 视频id
     * @return
     */
    @PostMapping("/{id}/comments")
    public ResponseEntity<Void> saveComments(@RequestBody Map<String, String> param,
                                             @PathVariable("id") String videoId) {
        String content = param.get("comment");      //评论内容
        videoService.saveComment(videoId, content);     // 保存评论
        return ResponseEntity.ok(null);
    }

    /**
     * @description 评论点赞
     * @param videoCommentId    视频评论的id
     * @return
     */
    @PostMapping("/comments/{id}/like")
    public ResponseEntity<Long> commentsLikeComment(@PathVariable("id") String videoCommentId) {
        Long likeCount = quanZiService.likeComment(videoCommentId , CommentPublishIdType.COMMENT);
        return ResponseEntity.ok(likeCount);
    }

    /**
     * @description 评论取消点赞
     * @param videoCommentId  视频评论的id
     * @return
     */
    @PostMapping("/comments/{id}/dislike")
    public ResponseEntity<Long> disCommentsLikeComment(@PathVariable("id") String videoCommentId) {
        Long likeCount = quanZiService.disLikeComment(videoCommentId);
        return ResponseEntity.ok(likeCount);
    }

    /**
     * @description 用户关注
     * @param userId 关注的用户id
     * @return
     */
    @PostMapping("/{id}/userFocus")
    public ResponseEntity<Void> saveUserFocusComments(@PathVariable("id") Long userId) {
        videoService.saveUserFocusComments(userId);
        return ResponseEntity.ok(null);
    }

    /**
     * @description 用户取消关注
     * @param userId
     * @return
     */
    @PostMapping("/{id}/userUnFocus")
    public ResponseEntity<Void> saveUserUnFocusComments(@PathVariable("id") Long userId) {
        videoService.saveUserUnFocusComments(userId);
        return ResponseEntity.ok(null);
    }
}
